package top.glkj.hms.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.glkj.hms.entity.Role;
import top.glkj.hms.exception.DefineException;
import top.glkj.hms.service.RoleService;
import top.glkj.hms.utils.Result;

import java.util.List;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/29 14:46
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/system/role")
public class RoleManagementController {
    private final RoleService roleService;

    @Autowired
    public RoleManagementController(RoleService roleService) {
        this.roleService = roleService;
    }


    @GetMapping
    @ApiOperation("获取所有角色")
    public Result getRole() {
        return roleService.getAll();
    }

    @ApiOperation("分页获取所有用户")
    @GetMapping("/{size}/{page}")
    public Result getRole(@PathVariable(name = "size") Integer size, @PathVariable(name = "page") Integer page) {
        return roleService.getRolesByPageAndSize(page, size);
    }

    @ApiOperation("添加角色")
    @PostMapping
    public Result addRole(Role role) {
        return roleService.addRole(role);
    }

    @ApiOperation("更新角色")
    @PutMapping
    public Result updateRole(Role role) {
        return roleService.updateRole(role);
    }

    @ApiOperation("删除角色")
    @DeleteMapping
    public Result remove(Long id) {
        return roleService.remove(new Role(id));
    }

    @ApiOperation("彻底删除角色")
    @DeleteMapping("/delete")
    public Result delete(Long id) {
        return roleService.delete(new Role(id));
    }

    @ApiOperation("角色添加权限")
    @PostMapping("/permission")
    public Result roleAddPermission(Long rid, List<Long> pidList) throws DefineException {
       return roleService.roleAddPermissions(rid,pidList);
    }

    @ApiOperation("角色修改权限")
    @PutMapping("/permission")
    public Result roleUpdatePermission(Long rid, List<Long> pidList) throws DefineException {
        return roleService.roleUpdatePermissions(rid,pidList);
    }

    @ApiOperation("角色删除权限")
    @DeleteMapping("/permission")
    public Result roleDeletePermission(Long rid, List<Long> pidList) throws DefineException {
        return roleService.roleDeletePermissions(rid,pidList);
    }
    @ApiOperation("获取角色信息，根据角色名称")
    @GetMapping("/name")
    public Result getRoleByName(String name){return roleService.getRoleByName(name);}
}
